package com.xique.door.controller;

import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.BuildingDevice;
import com.xique.door.service.IBuildingDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: caogq
 * @Date: 2022/1/28 15:41
 * @Description:
 */
@Controller
@RequestMapping("/oms/device")
public class BuildingDeviceController extends BaseController
{
    private String prefix = "oms/device";

    @Autowired
    private IBuildingDeviceService buildingDeviceService;

    @PreAuthorize(hasPermi="oms:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询楼栋设备中间列表
     */
    @PreAuthorize(hasPermi="oms:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BuildingDevice buildingDevice)
    {
        startPage();
        List<BuildingDevice> list = buildingDeviceService.selectBuildingDeviceList(buildingDevice);
        return getDataTable(list);
    }

    /**
     * 导出楼栋设备中间列表
     */
    @PreAuthorize(hasPermi="oms:device:export")
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response,BuildingDevice buildingDevice) throws IOException {
        List<BuildingDevice> list = buildingDeviceService.selectBuildingDeviceList(buildingDevice);
        ExcelUtil<BuildingDevice> util = new ExcelUtil<BuildingDevice>(BuildingDevice.class);
        util.exportExcel(response,list, "device");
    }

    /**
     * 新增楼栋设备中间
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存楼栋设备中间
     */
    @PreAuthorize(hasPermi="oms:device:add")
    @Log(title = "楼栋设备中间", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BuildingDevice buildingDevice)
    {
        return toAjax(buildingDeviceService.insertBuildingDevice(buildingDevice));
    }

    /**
     * 修改楼栋设备中间
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BuildingDevice buildingDevice = buildingDeviceService.selectBuildingDeviceById(id);
        mmap.put("buildingDevice", buildingDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存楼栋设备中间
     */
    @PreAuthorize(hasPermi="oms:device:edit")
    @Log(title = "楼栋设备中间", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BuildingDevice buildingDevice)
    {
        return toAjax(buildingDeviceService.updateBuildingDevice(buildingDevice));
    }

    /**
     * 删除楼栋设备中间
     */
    @PreAuthorize(hasPermi="oms:device:remove")
    @Log(title = "楼栋设备中间", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(buildingDeviceService.deleteBuildingDeviceByIds(ids));
    }
}
